clear all
set more off

****************************************************************
//// 09-BUILD_OFFENDING: CONSTRUCT OFFENSE DATA FOR DRIVERS ////
***************************************************************


***************************************************
/// KEEP ONLY VALID FLORIDA DLs FROM ALL STOPS ////
***************************************************

use "${temp}/process-citation", clear

replace DLstate=trim(upper(DLstate))
keep if DLstate=="FL"
replace DLnumber=trim(upper(DLnumber))
keep if length(DLnumber)==13

gen firstchar=substr(DLnumber,1,1)
gen firstalpha=regexm(firstchar,"^[A-Z]")

gen restchar=substr(DLnumber,2,.)
gen restalpha=regexm(restchar,"^[A-Z]")

keep if firstalpha==1 & restalpha==0
drop firstchar firstalpha restchar restalpha


*******************************
/// STORE BASIC STOP INFO ////
*******************************

gen citationnumber = CitationNumber
gen citation = 1
gen crash = 0
gen date = OffenseDate
gen dlnumber = DLnumber
gen viol = Viol_Code
gen issueagency = IssueAgencyType
gen countynum = CountyNum
gen speeddiff = SpeedDiff
keep dlnumber date citation crash citationnumber viol issueagency countynum speeddiff
order dlnumber date citation crash citationnumber viol issueagency countynum speeddiff


******************************
//// STORE TEMPORARY LIST ////
******************************

qui compress
saveold "${temp}/stops-list", replace


*****************************************************
//// CONSTRUCT DRIVER LIST 					  ////
//// DRIVERS WITH ANY FHP-ISSUED TICKET         ////
//// ALREADY KEPT ONLY VALID FL ABOVE           ////
****************************************************

keep if issueagency==1
keep dlnumber
duplicates drop
qui compress
saveold "${temp}/driver-list", replace


*****************************************************
//// SAVE OVER STOPS LIST WITH ONLY DRIVER LIST ////
*****************************************************

merge 1:m dlnumber using "${temp}/stops-list", keep(3) nogen
qui compress
saveold "${temp}/stops-list", replace



**************************************
//// ATTACH VIOLATION INFORMATION ////
**************************************

gen viol_code = viol
merge m:1 viol_code using "${temp}/violation", keep(1 3) nogen

drop viol_code viol_description viol_flag viol_group_old

/// ADJUST FINES ///
* FIRST: CORRECT SPEEDING BASED ON SPEED *
replace viol_fine = 0 if viol==575 & speeddiff>=0 & speeddiff<=5
replace viol_fine = 25 if viol==575 & speeddiff>=6 & speeddiff<=9
replace viol_fine = 100 if viol==575 & speeddiff>=10 & speeddiff<=14
replace viol_fine = 150 if viol==575 & speeddiff>=15 & speeddiff<=19
replace viol_fine = 175 if viol==575 & speeddiff>=20 & speeddiff<=29
replace viol_fine = 250 if viol==575 & speeddiff>=30 
replace viol_fine = 60 if viol==575 & mi(viol_fine)
* ADJUST POINTS FOR SPEEDING *
replace viol_points = 3 if viol==575 & speeddiff<=15
replace viol_points = 4 if viol==575 & speeddiff>=16
replace viol_points = 3 if viol==575 & mi(viol_points)
* ADD DISTRIBUTION AMOUNTS *
replace viol_fine = viol_fine + 98

* GET RID OF LABELS? *
foreach x of varlist viol_disp - viol_fine {
label var `x' ""
}


qui compress
saveold "${temp}/stops-list", replace



*********************************
///// NOW WORK ON CRASHES //////
*********************************

use "${temp}/driver-list", clear
merge 1:m dlnumber using "${temp}/process-crash", keep(3) nogen

gen citation=0
gen crash=1

drop _merge
ren reportnumber crash_number
ren form_type crash_form
ren codeable crash_codeable
ren part crash_part
ren driveraction crash_action
ren severity crash_severity
ren multicar crash_multicar

keep dlnumber date citation crash crash_number crash_form crash_codeable crash_part crash_action crash_severity crash_multicar
order dlnumber date citation crash crash_number crash_form crash_codeable crash_part crash_action crash_severity crash_multicar

qui compress
saveold "${temp}/crash-list", replace



***************************************************
///// APPEND CITATIONS AND CRASHES TOGETHER //////
*************************************************** 

use "${temp}/stops-list", clear
qui append using "${temp}/crash-list"
replace citation=0 if mi(citation)
replace crash=0 if mi(crash)
drop if mi(date)


label var dlnumber "DL Number"
label var date "Date"
label var citation "Citation (From UTC)"
label var crash "Crash (From Crashes)"
label var citationnumber "Citation ID"
label var viol "Violation Code"
label var issueagency "Issue Agency Type"
label var countynum "County"
label var speeddiff "MPH over Posted"
replace viol_disp = trim(upper(viol_disp))
label var viol_disp "Violation Disposition"
label var viol_sus "Violation Suspension"
label var viol_class "Violation Class"
label var viol_fine "Violation Fine"
label var viol_points "Violation Points" 


sort dlnumber date citation
label data "All Offending for 5.4M FLORIDA Drivers"
qui compress
saveold "${out}/1-offending", replace

rm "${temp}/driver-list.dta"
rm "${temp}/stops-list.dta"
rm "${temp}/crash-list.dta"











